package cn.icare.cms.controller.restful;

import cn.icare.cms.PromotionService;
import cn.icare.cms.model.ro.AddPromotionItemRo;
import cn.icare.cms.model.ro.AddPromotionRO;
import cn.icare.cms.model.ro.EditPromotionRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/promotionitem")
@Api(tags = "REST促销商品管理")
public class PromotionItemController extends BaseController {
    @Resource
    private PromotionService promotionService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/promotion
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "savePromotionItemFall")

    @ApiOperation(value = "保存促销商品", notes = "保存促销商品")
    public ResponseEntity<RespMsgVO> savePromotionItem(@RequestBody AddPromotionItemRo ro) {

        boolean result=this.promotionService.savePromotionItem(ro.getPromId(),ro.getItemList());
        if (result)
        {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }



    /**
     * URL:http://{ip}:{port}/rest/promotionitem/{id}
     * method:GET
     *
     * @param promId
     * @return
     */
    @GetMapping("/{promId}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个促销商品列表", notes = "查询单个促销商品列表")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("promId") Integer promId
            ,@RequestParam(name = "title", required = false) String title) {
        return super.getRespOk(this.promotionService.getPromItemList(promId,title));
    }

    /**
     * URL:http://{ip}:{port}/rest/promotion?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询不在促销的商品列表", notes = "查询不在促销的商品列表")
    public ResponseEntity<RespMsgVO> getList(
            @RequestParam(name = "cid", required = false) Long cid,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize

    ) {
        return super.getRespOk(this.promotionService.getNoPromItemList(cid
                , title, pageNum, pageSize));
    }
    //#region fallback methods
    private ResponseEntity<RespMsgVO> savePromotionItemFall(AddPromotionItemRo ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer promId,String title) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Long cid, String title, Integer pageNum, Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
